package com.example.park

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.base.entity.ParkEntityItem
import com.example.park.databinding.ItemParkBinding

class ParkPageAdapter:PagingDataAdapter<ParkEntityItem,ParkPageAdapter.ParkViewHolder>(diff) {
    companion object{
        val diff = object :DiffUtil.ItemCallback<ParkEntityItem>(){
            override fun areItemsTheSame(
                oldItem: ParkEntityItem,
                newItem: ParkEntityItem
            ): Boolean {
                return oldItem.id==newItem.id
            }

            override fun areContentsTheSame(
                oldItem: ParkEntityItem,
                newItem: ParkEntityItem
            ): Boolean {
                return oldItem==newItem
            }
        }
    }

    class ParkViewHolder(itemView:View):ViewHolder(itemView){
        val binding = DataBindingUtil.bind<ItemParkBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        var item = getItem(position)!!
        holder.binding?.info = item
        holder.binding?.parkSpaceStatus?.apply {
            when(item.spaceStatus){
                "空闲"->{
                    this.setTextColor(Color.WHITE)
                    this.setText("空闲")
                    this.setBackgroundResource(R.drawable.bg_green)
                }
                "占用"->{
                    this.setTextColor(Color.WHITE)
                    this.setBackgroundResource(R.drawable.bg_red)
                }
                "维护中"->{
                    this.setTextColor(Color.WHITE)
                    this.setBackgroundResource(R.drawable.bg_yellow)
                }
                else->{
                    this.setTextColor(Color.WHITE)
                    this.setBackgroundResource(R.drawable.bg_yellow)
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_park, null)
        return ParkViewHolder(view)
    }
}